package com.haixiaoke.flat.lock.controller;

import com.haixiaoke.auth.AuthCheck;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.common.core.page.TableDataInfo;
import com.haixiaoke.saas.lock.domain.DeviceLock;
import com.haixiaoke.saas.lock.service.IDeviceLockService;
import com.haixiaoke.saas.system.BaseController;
import com.haixiaoke.token.FrontTokenComponent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 设备锁Controller
 *
 * @author panda
 * @date 2025-06-26
 */
@RestController
@RequestMapping("/front/flat/lock/lock")
public class FlatDeviceLockController extends BaseController {
    @Autowired
    private IDeviceLockService deviceLockService;
    @Autowired
    private FrontTokenComponent frontTokenComponent;

    /**
     * 查询设备锁列表
     */
    @GetMapping("/page")
    public TableDataInfo page(DeviceLock deviceLock) {
        startPage();
        deviceLock.setConUserId(frontTokenComponent.getUserIdNotNull());
        List<DeviceLock> list = deviceLockService.flatSelectDeviceLockList(deviceLock);
        return getDataTable(list);
    }

    /**
     * 获取设备锁详细信息
     */
    @GetMapping(value = "/{lockId}")
    public AjaxResult getInfo(@PathVariable("lockId") Long lockId) {
        return success(deviceLockService.selectDeviceLockByLockId(lockId));
    }

    /**
     * 开锁记录
     */
    @AuthCheck
    @GetMapping("/unlockingRecord")
    public TableDataInfo unlockingRecord(@RequestParam(value = "lockId") Long lockId,
                                         @RequestParam(value = "unlockTime", required = false) Date unlockTime,
                                         @RequestParam(value = "userName", required = false) String userName,
                                         @RequestParam(value = "unlockType", required = false) Integer unlockType,
                                         @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                         @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        return deviceLockService.unlockingRecord(lockId, unlockTime, userName, unlockType, pageNum, pageSize);
    }
}
